export class ListNode {
    val: number
    next: ListNode | null
    constructor(val?: number, next?: ListNode | null) {
        this.val = (val===undefined ? 0 : val)
        this.next = (next===undefined ? null : next)
    }
}

export function transArryToLinkList(values: number[]): ListNode {
    let firstNode: ListNode;
    let currNode: ListNode;
    values.forEach(value => {
        if (!firstNode) {
            firstNode = new ListNode(value);
            currNode = firstNode;
        } else {
            currNode.next = new ListNode(value);
            currNode = currNode && currNode.next;
        }
    });
    // @ts-ignore
    return firstNode as ListNode;
}

export function transToArry(headNode: ListNode | null) {
    const valueArray = [];
    while(headNode && (typeof headNode.val === 'number')) {
        valueArray.push(headNode.val);
        headNode = headNode.next;
    }
    return valueArray;
}